package service.impl;

import java.util.ArrayList;
import java.util.List;
import persistence.dao.DaoManager;
import persistence.dao.RoutsDao;
import persistence.dao.base.ConnectionBehavior;
import persistence.dao.base.DaoCommand;
import persistence.entity.RoutsEntity;
import model.IRouts;
import model.Routs;
import service.RoutsService;

public class RoutsServiceImpl implements RoutsService {

	private DaoManager daoManager;

	public RoutsServiceImpl(DaoManager daoManager) {
		this.daoManager = daoManager;
	}

	protected DaoManager getDaoManager() {
		return daoManager;
	}

	@Override
	public List<IRouts> getRoutsId(final String searchFrom,
			final String searchTo, final String date_routs) {
		return (List<IRouts>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				final List<RoutsEntity> listRoutsEntity = routsDao.getRoutsId(
						searchFrom, searchTo, date_routs);
				final List<IRouts> listRouts = new ArrayList<IRouts>();
				for (final RoutsEntity routsEntity : listRoutsEntity) {
					listRouts.add(convertToModel(routsEntity));
				}
				return listRouts;
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IRouts getRoutsById(final int id) {
		return (IRouts) 
		getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				return convertToModel(routsDao.routsById(id));
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<IRouts> getRouts() {
		return (List<IRouts>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				final List<RoutsEntity> listRoutsEntity = routsDao.getRouts();
				final List<IRouts> listRouts = new ArrayList<IRouts>();
				for (final RoutsEntity routsEntity : listRoutsEntity) {
					listRouts.add(convertToModel(routsEntity));
				}
				return listRouts;
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public List<IRouts> getRoutsStantion() {
		return (List<IRouts>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				final List<RoutsEntity> listRoutsEntity = routsDao.getRoutsStantion();
				final List<IRouts> listRouts = new ArrayList<IRouts>();
				for (final RoutsEntity routsEntity : listRoutsEntity) {
					listRouts.add(convertToModel(routsEntity));
				}
				return listRouts;
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IRouts setRouts(final String add_routs, final String add_date) {
		return (IRouts) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				routsDao.setRouts(add_routs, add_date);
				return null; 
			}
		}, ConnectionBehavior.CLOSE);

	}

	@Override
	public IRouts setRoutsUpdate(final int[] id_update,
			final String[] name_routs, final String[] date_routs) {
		return (IRouts) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				routsDao.setRoutsUpdate(id_update, name_routs, date_routs);
				return null; 
			}
		}, ConnectionBehavior.CLOSE);
	}

	public List<IRouts> getRoutsById(final int[] id_update) {
		return (List<IRouts>) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				final List<RoutsEntity> listRoutsEntity = routsDao.getRoutsById(id_update);
				final List<IRouts> listRouts = new ArrayList<IRouts>();
				for (final RoutsEntity routsEntity : listRoutsEntity) {
					listRouts.add(convertToModel(routsEntity));
				}
				return listRouts;
			}
		}, ConnectionBehavior.CLOSE);
	}

	@Override
	public IRouts delete(final int[] id_update) {
		return (IRouts) getDaoManager().command(new DaoCommand() {
			public Object execute(final DaoManager manager) {
				final RoutsDao routsDao = manager.getDao(RoutsDao.class);
				routsDao.delete(id_update);
				return null; 
			}
		}, ConnectionBehavior.CLOSE);
	}

	private IRouts convertToModel(RoutsEntity routsEntity) {
		IRouts routs = null;
		if (routsEntity != null) {
			routs = new Routs(
			routsEntity.getId_city(), routsEntity.getName_city(),
					routsEntity.getId(), routsEntity.getId_stantion_city(),
					routsEntity.getId_stantion_routs(),
					routsEntity.getId_routs(), routsEntity.getName_routs(),
					routsEntity.getDate_routs());
		}
		return routs;
	}
}
